# Things IoT Platform Makefile

.PHONY: help build run test clean docker-up docker-down migrate dev

# 默认目标
help:
	@echo "Things IoT Platform 构建工具"
	@echo ""
	@echo "可用命令:"
	@echo "  build        - 构建应用程序"
	@echo "  run          - 运行应用程序"
	@echo "  test         - 运行测试"
	@echo "  clean        - 清理构建文件"
	@echo "  docker-up    - 启动Docker服务"
	@echo "  docker-down  - 停止Docker服务"
	@echo "  migrate      - 运行数据库迁移"
	@echo "  dev          - 启动开发环境"
	@echo "  lint         - 运行代码检查"
	@echo "  fmt          - 格式化代码"

# 构建应用程序
build:
	@echo "构建应用程序..."
	go build -o bin/main_backend main.go

# 运行应用程序
run: build
	@echo "启动应用程序..."
	./bin/main_backend

# 运行测试
test:
	@echo "运行测试..."
	go test -v ./...

# 清理构建文件
clean:
	@echo "清理构建文件..."
	rm -rf bin/
	go clean

# 启动Docker服务
docker-up:
	@echo "启动Docker服务..."
	docker-compose up -d

# 停止Docker服务
docker-down:
	@echo "停止Docker服务..."
	docker-compose down

# 重启Docker服务
docker-restart: docker-down docker-up

# 查看服务日志
logs:
	docker-compose logs -f app

# 查看所有服务日志
logs-all:
	docker-compose logs -f

# 运行数据库迁移
migrate:
	@echo "运行数据库迁移..."
	# 这里可以添加数据库迁移命令
	@echo "迁移完成"

# 启动开发环境
dev: docker-up
	@echo "启动开发环境..."
	@echo "等待服务启动..."
	sleep 3
	@echo "运行应用程序..."
	go run main.go

# 代码检查
lint:
	@echo "运行代码检查..."
	# 这里可以添加golangci-lint等工具
	go vet ./...

# 格式化代码
fmt:
	@echo "格式化代码..."
	go fmt ./...

# 安装依赖
deps:
	@echo "安装依赖..."
	go mod download
	go mod tidy

# 生成Swagger文档
swagger:
	@echo "生成Swagger文档..."
	# 这里可以添加swag init等命令
	@echo "Swagger文档生成完成"

# 健康检查
health:
	@echo "检查服务健康状态..."
	curl -f http://localhost:8080/health || echo "服务未运行"

# 数据库连接测试
db-test:
	@echo "测试数据库连接..."
	# 这里可以添加数据库连接测试命令
	@echo "数据库连接测试完成"

# 性能测试
bench:
	@echo "运行性能测试..."
	go test -bench=. -benchmem ./...

# 代码覆盖率测试
coverage:
	@echo "运行代码覆盖率测试..."
	go test -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "覆盖率报告已生成: coverage.html"

# 安全扫描
security:
	@echo "运行安全扫描..."
	# 这里可以添加gosec等安全扫描工具
	@echo "安全扫描完成"

# 构建Docker镜像
docker-build:
	@echo "构建Docker镜像..."
	docker-compose build

# 部署到生产环境
deploy: docker-build
	@echo "部署到生产环境..."
	# 这里可以添加生产环境部署命令
	@echo "部署完成"

# 备份数据库
backup:
	@echo "备份数据库..."
	docker-compose exec postgres pg_dump -U postgres things_iot > backup_$(shell date +%Y%m%d_%H%M%S).sql
	@echo "数据库备份完成"

# 恢复数据库
restore:
	@echo "恢复数据库..."
	# 这里需要指定备份文件
	@echo "请指定备份文件: make restore FILE=backup_file.sql"
	# docker-compose exec -T postgres psql -U postgres things_iot < $(FILE)

# 查看系统状态
status:
	@echo "=== 系统状态 ==="
	@echo "Docker容器状态:"
	docker-compose ps
	@echo ""
	@echo "应用程序状态:"
	curl -s http://localhost:8080/health | jq . 2>/dev/null || curl -s http://localhost:8080/health
	@echo ""
	@echo "数据库连接:"
	docker-compose exec postgres psql -U postgres -d things_iot -c "SELECT version();" 2>/dev/null || echo "数据库未连接"
	@echo ""
	@echo "Redis连接:"
	docker-compose exec redis redis-cli ping 2>/dev/null || echo "Redis未连接"

# 清理所有Docker资源
clean-all: docker-down
	@echo "清理所有Docker资源..."
	docker system prune -f
	docker volume prune -f

# 开发环境设置
setup: deps docker-up migrate
	@echo "开发环境设置完成!"
	@echo "运行 'make dev' 启动开发服务器"